export function createTreeData(original, childIdProp, parentIdProp) {
    let index = 0;
    const tree = [];
    let item = original[0];
    const treeObj = {};
    const len = original.length;
    let id, parent, parentId;

    // 生成treeObj字典
    original.forEach(row => (treeObj[row[childIdProp]] = row));

    while (index < len) {
        item = original[index++];
        id = item[childIdProp];
        parentId = item[parentIdProp];
        if (parentId || parentId === 0) {
            parent = treeObj[parentId];
            if (parent) {
                parent.children ? parent.children.push(item) : (parent.children = [item]);
            }
        } else {
            tree.push(item);
        }
    }
    return tree;
}

// 拿到后台List
const testData = [
    { title: 'a', id: 0, parentId: null, checked: false },
    { title: 'b', id: 1, parentId: 0, checked: false },
    { title: 'c', id: 2, parentId: 1, checked: false },
    { title: 'd', id: 3, parentId: 0, checked: false },
    { title: 'e', id: 4, parentId: 3, checked: false },
    { title: 'f', id: 5, parentId: 0, checked: false }
];

// 拿到选中的id的数组
const selectedElement = testData.filter(ele => ele.checked);

// 给这个对象加一些自定属性
testData.map(ele => {
    ele['active'] = false;
    return ele;
});
